// import { EventKey } from '../common/Key';
import { EventKey } from "../common_for_guosheng/Key";
import {
    GameGSState
} from '../game_for_guosheng/GameGSState';
import {
    GameGSData
} from '../game_for_guosheng/GameGSData';
cc.Class({
    extends: require('BaseGSPanel'),

    properties: {
        labScoreSucc: cc.Label,
        labScoreFail: cc.Label,
        nodSucc: cc.Node,
        nodFail: cc.Node,
    },

    onLoad() {
        this.dnmd();
    },

    onEnter(args, action = true) {
        this.dnmdonEnter(args, action);

        this._showScoreEffect();

        if (args.result) {
            this._saveFuckBestScore();
            this._showSucc();
        } else {
            GameGSData.saveLastGame(null);
            this._showFail();
            F.Game.gameAudioGS.playFail();
        }
    },
    _saveFuckBestScore() {
        GameGSData.setBestLevel(GameGSState.getLevel());
        GameGSData.setBestScore(GameGSState.getScore());
    },
    _showScoreEffect() {
        let score = GameGSState.getScore();
        let length = String(score).length;
        this.labScoreSucc.string = '0';
        this.labScoreFail.string = '0';
        let step = Math.floor(score / 10);
        let tScore = 0;
        let tFunc = () => {
            if (tScore >= score) {
                this.unschedule(tFunc);
                return;
            }
            tScore += step;
            if (tScore > score) {
                tScore = score;
            }
            let string = F.Utils.prefixZero(tScore, length);
            this.labScoreSucc.string = string;
            this.labScoreFail.string = string;
        }
        this.schedule(tFunc, 0.05)
    },

    _showSucc() {
        this.nodSucc.active = true;
        this.nodFail.active = false;
    },

    _showFail() {
        this.nodSucc.active = false;
        this.nodFail.active = true;
    },

    onExit(args) {
        this.dnmdonExit(args);
    },

    FunMudPListener(button) {
        let name = button.node.name;
        if (name == 'btnClose') {
            this._onForGuoShenClose();
        } else if (name == 'btnContinue' || name == 'btnContinueSucc' ) {
            F.OnEvent.fire(EventKey.EVENT_RECOVER_CELL);
            this.enterArgs.func1 && this.enterArgs.func1();
            this._onForGuoShenClose();
        } else if (name == 'btnHome') {
            // 回收所有星星节点
            F.OnEvent.fire(EventKey.EVENT_RECOVER_CELL);
            F.OnEvent.fire(EventKey.EVENT_SAVE_GAME_DATA);
            // 返回主界面
            F.Game.loadScene('SceneHome');
            F.Game.gameAudioGS.stopBgm();
            F.Audio.clearBgm();
        }

    },
    _onTouchStart() {},

});